defaultTasks 'run'

apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'jetty'

sourceCompatibility = '1.7'

configurations {
    jetty8
    jflex
}

task wrapper(type: Wrapper) {
    gradleVersion = '1.5'
}

repositories {
    mavenCentral()
    mavenLocal()
}

ext {
    src          = new File(project.projectDir, 'src/main/java')
    dist         = new File(project.projectDir, 'dist')
    generatedsrc = new File(project.projectDir, 'src/jflex/java')
    workingdirs  = [project.buildDir, generatedsrc, dist]
}

dependencies {
    String luceneVersion = '3.6.1'
    String jettyVersion = '8.1.8.v20121106'
    List common = [
            "org.apache.lucene:lucene-core:$luceneVersion",
            "org.apache.lucene:lucene-spellchecker:$luceneVersion",
            "org.apache.bcel:bcel:5.2",
            "org.apache.ant:ant:1.8.4",
            "com.ibm.icu:icu4j:50.1.1",
    ]
    jflex "de.jflex:maven-jflex-plugin:1.4.3"
    compile common
    compile "javax:javaee-web-api:6.0"
    runtime common
    testCompile "junit:junit:4.8.2"
    jetty8 "org.mortbay.jetty:jetty-runner:$jettyVersion",
            "jstl:jstl:1.2"

}

task cleanup << {
    delete project.ext.workingdirs
}

task prepare << {
    project.ext.workingdirs.each {
        it.mkdirs()
    }
}

task jflex(dependsOn: prepare) << {
    ant.taskdef(name: 'jflex', classname: 'JFlex.anttask.JFlexTask', classpath: configurations.jflex.asPath)

    fileTree(dir: project.ext.src).include('**/*.lex').each {
        ant.jflex(destdir: project.ext.generatedsrc, file: it)
    }
}

compileJava {
    dependsOn jflex
}

clean {
    dependsOn cleanup
}

sourceSets {
    main {
        java {
            srcDir project.ext.src
            srcDir project.ext.generatedsrc
        }
        resources {
            srcDir project.ext.src
        }
    }
}

jettyRunWar {
    if (project.hasProperty('httpPort')) {
        httpPort = project.httpPort as Integer
    }
}

jar {
    destinationDir project.ext.dist
    manifest {
        attributes('Main-Class': 'org.watermint.sourcecolon.org.opensolaris.opengrok.index.Indexer')
    }
    from configurations.runtime.collect { it.isDirectory() ? it : zipTree(it) }
}

task runIndexer(dependsOn: jar) << {
    String target = project.ext.src.absolutePath
    if (project.hasProperty('target')) {
        if (file(project.target).isDirectory()) {
            target = project.target
        } else {
            logger.warn("${project.target} is not a directory. use $target as default.")
        }
    }

    ant.java(jar: jar.archivePath, fork: true) {
        jvmarg(value: '-Dfile.encoding=UTF-8')
        arg(value: '-s')
        arg(value: target)
    }
}

task prepareWar(dependsOn: jar) << {
    File destWar = new File(project.buildDir, 'war')
    File baseWar = new File(project.projectDir, 'web')
    copy {
        from baseWar
        into destWar
    }
    copy {
        from jar.archivePath
        into new File(destWar, 'WEB-INF/lib')
    }
}

war {
    dependsOn prepareWar
    destinationDir project.ext.dist
    from new File(project.buildDir, 'war')
}

task runJetty8(type: JavaExec, dependsOn: war) {
    main = "org.mortbay.jetty.runner.Runner"
    args = [war.archivePath]
    jvmArgs '-Dfile.encoding=UTF-8'
    classpath configurations.jetty8
}

task all(dependsOn: [build, war, runIndexer]) {
}

task run(dependsOn: [all, runJetty8]) {
}
